﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Web;
using FTBS.Models.Entities;
using FTBS.Models.Entities.Concert;
using System.Data.Objects.SqlClient;
using System.Data.Entity;

namespace FTBS.Models.DAO
{
    public class ScheduleConcertEntityDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();
        public List<ScheduleConcertEntity> LoadSchedules()
        {
            List<ScheduleConcertEntity> listResult = new List<ScheduleConcertEntity>();
            DateTime dateTime = DateTime.Today;
            var resultQuery = from schedule in _entities.Schedules
                              join film in _entities.Films on schedule.FilmId equals film.FilmId
                              join room in _entities.Rooms on schedule.RoomId equals room.RoomId
                              join theater in _entities.Theaters on schedule.TheaterId equals theater.TheaterId
                              //join st in _entities.SettingTheaters on schedule.TheaterId equals st.TheaterId
                              join showTime in _entities.ShowTimes on schedule.ShowTimeId equals showTime.ShowTimeId
                              join user in _entities.Users on schedule.UserName equals user.UserName
                              join promotion in _entities.Promotions on schedule.PromotionId equals promotion.PromotionId into pro
                              from promotion in pro.DefaultIfEmpty()
                              where
                              (((theater.DeadlineCreateTicket == 0) && (schedule.Date == dateTime) && (showTime.Time > dateTime.TimeOfDay)) ||
                              ((theater.DeadlineCreateTicket > 0) && (schedule.Date > EntityFunctions.AddDays(dateTime, (theater.DeadlineCreateTicket - 1)))) ||
                              ((theater.DeadlineCreateTicket == 0) && (schedule.Date > dateTime)))
                                  && schedule.IsDeleted == false
                                  && film.IsDeleted == false
                                  && room.IsDeleted == false
                                  && theater.IsDeleted == false
                                  && showTime.IsDeleted == false
                                  && user.IsActive == true
                                  && user.IsDeleted == false
                              select new
                              {
                                  Schedules = schedule,
                                  FilmName = film.Name,
                                  RoomName = room.RoomName,
                                  TheaterName = theater.TheaterName,
                                  DateForShow = schedule.Date,
                                  TimeForShow = showTime.Time,
                                  //PromotionName = promotion.PromotionName,
                                  //PromotionRate = promotion.Rate,
                                  PeopleCreate = user.UserName,
                              };
            foreach (var item in resultQuery.ToList())
            {
                ScheduleConcertEntity scheduleEntity = new ScheduleConcertEntity();
                scheduleEntity.ScheduleId = item.Schedules.ScheduleId;
                scheduleEntity.FilmId = item.Schedules.FilmId;
                scheduleEntity.RoomId = item.Schedules.RoomId;
                scheduleEntity.IsActive = item.Schedules.IsActive;
                scheduleEntity.ShowTimeId = item.Schedules.ShowTimeId;
                scheduleEntity.Date = item.Schedules.Date;
                scheduleEntity.TheaterId = item.Schedules.TheaterId;
                scheduleEntity.PromotionId = item.Schedules.PromotionId;
                scheduleEntity.UserName = item.Schedules.UserName;
                scheduleEntity.FilmName = item.FilmName;
                scheduleEntity.RoomName = item.RoomName;
                scheduleEntity.TheaterName = item.TheaterName;
                
                scheduleEntity.TimeForShow = item.TimeForShow.ToString(@"hh\:mm");
                //scheduleEntity.PromotionName = item.PromotionName;
                //scheduleEntity.PromotionRate = Convert.ToDouble(item.PromotionRate);
                scheduleEntity.PeopleCreate = item.PeopleCreate;
                listResult.Add(scheduleEntity);
            }
            return listResult.ToList();

        }
    }
}